import logging
from pathlib import Path
from datetime import datetime, timezone
from performance.runner.constants import GENERAL_LOG_FILE, SAMPLE_DATA_DIR, DETAIL_DATA_DIR


def create_general_logger(performance_log_path: Path) -> logging.Logger:
    """
    设置并返回配置好的日志记录器。
    """
    # 创建日志目录
    performance_log_path.mkdir(parents=True, exist_ok=True)
    # 创建日志记录器
    general_logger = logging.getLogger('performance_test')
    general_logger.setLevel(logging.INFO)
    # 创建文件处理器
    log_file = performance_log_path / GENERAL_LOG_FILE
    file_handler = logging.FileHandler(log_file, mode='a', encoding='utf-8')
    file_handler.setLevel(logging.INFO)
    # 创建日志格式
    formatter = logging.Formatter(
        '%(asctime)s [%(levelname)s]: %(message)s'
    )
    file_handler.setFormatter(formatter)
    # 添加处理器
    general_logger.addHandler(file_handler)
    return general_logger


def create_step_logger(monitor_log_path: str, feature_category: str) -> logging.Logger:
    """
    创建步骤日志记录器
    """
    step_logger_dir = monitor_log_path / feature_category
    # 确保日志目录存在
    step_logger_dir.mkdir(parents=True, exist_ok=True)

    # 创建样本数据日志记录器
    monitor_type = str(monitor_log_path).split('/')[-1]
    step_logger = logging.getLogger(f'{monitor_type}_{feature_category}_step')
    step_logger.setLevel(logging.INFO)

    # 创建文件处理器
    log_file = step_logger_dir / f"{feature_category}.log"
    file_handler = logging.FileHandler(log_file, mode='a', encoding='utf-8')
    file_handler.setLevel(logging.INFO)

    # 创建日志格式
    formatter = logging.Formatter('%(asctime)s [%(levelname)s]: %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
    file_handler.setFormatter(formatter)

    # 添加处理器
    step_logger.addHandler(file_handler)
    return step_logger


def create_sample_logger(monitor_log_path: Path, feature_category: str) -> logging.Logger:
    """创建样本数据日志记录器"""
    # 拼接log输出文件路径
    feature_data_dir = monitor_log_path / feature_category
    
    # 确保日志目录存在
    feature_data_dir.mkdir(parents=True, exist_ok=True)

    # 创建样本数据日志记录器
    monitor_type = str(monitor_log_path).split('/')[-1]
    sample_logger = logging.getLogger(f'{monitor_type}_{feature_category}_sample')
    sample_logger.setLevel(logging.INFO)

    # 创建文件处理器
    log_file = feature_data_dir / f"{SAMPLE_DATA_DIR}.log"
    file_handler = logging.FileHandler(log_file, mode='w', encoding='utf-8')
    file_handler.setLevel(logging.INFO)

    # 创建日志格式
    formatter = logging.Formatter('%(message)s')
    file_handler.setFormatter(formatter)

    # 添加处理器
    sample_logger.addHandler(file_handler)
    return sample_logger


def create_detail_logger(monitor_log_path: Path, feature_category: str, test_case_name: str) -> logging.Logger:
    """创建详细信息日志记录器"""
    # 拼接log输出文件路径
    detail_data_dir = monitor_log_path / feature_category / DETAIL_DATA_DIR
    
    # 确保日志目录存在
    detail_data_dir.mkdir(parents=True, exist_ok=True)

    # 创建日志记录器
    monitor_type = str(monitor_log_path).split('/')[-1]
    detail_logger = logging.getLogger(f'{monitor_type}_{feature_category}_detail_{test_case_name}')
    detail_logger.setLevel(logging.INFO)

    # 创建文件处理器
    timestamp = datetime.now(timezone.utc).astimezone().strftime("%Y%m%d_%H%M%S")
    log_file = detail_data_dir / f"{feature_category}_{test_case_name}_{timestamp}.log"
    file_handler = logging.FileHandler(log_file, mode='w', encoding='utf-8')
    file_handler.setLevel(logging.INFO)

    # 创建日志格式
    formatter = logging.Formatter('%(message)s')
    file_handler.setFormatter(formatter)

    # 添加处理器
    detail_logger.addHandler(file_handler)
    return detail_logger